1107C - Brutality - CodeForces Solution


greedy sortings two pointers *1300

Please click on ads to support us..

Python Code:

n, k = map(int, input().split())
a = list(map(int, input().split()))
s = list(input())
ans = 0
if n == k:
    print(sum(a))
else:
    i = 0
    while i < n:
        j = i + 1
        ss = [a[i]]
        while j < n and s[i] == s[j]:
            ss.append(a[j])
            j += 1
        ss.sort()
        m = sum(ss[-k:])
        ans += m
        i = j
    print(ans)

C++ Code:

#include <bits/stdc++.h>
using namespace std;

#define INF 1e18
#define nline "\n"
#define pb push_back
#define ppb pop_back
#define mp make_pair
#define ff first
#define ss second
#define PI 3.141592653589793238462
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
#define rep(i,a,b) for(long long i=a;i<b;i++)
#define rep_(i,a,b) for(long long i=a;i>=b;i--)
#ifndef ONLINE_JUDGE
#define debug(x) cerr << #x << " "; _print(x); cerr << endl;
#else
#define debug(x)
#endif

typedef long long ll;
typedef unsigned long long ull;
typedef long double lld;

void _print(ll t) { cerr << t; }
void _print(int t) { cerr << t; }
void _print(string t) { cerr << t; }
void _print(char t) { cerr << t; }
void _print(lld t) { cerr << t; }
void _print(double t) { cerr << t; }
void _print(ull t) { cerr << t; }

template <class T, class V> void _print(pair <T, V> p);
template <class T> void _print(vector <T> v);
template <class T> void _print(set <T> v);
template <class T, class V> void _print(map <T, V> v);
template <class T> void _print(multiset <T> v);
template <class T, class V> void _print(pair <T, V> p) {cerr << "{"; _print(p.ff); cerr << ","; _print(p.ss); cerr << "}";}
template <class T> void _print(vector <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T> void _print(set <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T> void _print(multiset <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T, class V> void _print(map <T, V> v) {cerr << "[ "; for (auto i : v) {_print(i); cerr << " ";} cerr << "]";}
/*--------------------------------------------------------------------------------------------------------------------------*/ 
void YES(){cout<<"YES"<<nline;}
void NO(){cout<<"NO"<<nline;}
void Neg1(){cout<<"-1"<<nline;}
/*--------------------------------------------------------------------------------------------------------------------------*/
void solve()
{
  ll n,k;
  cin>>n>>k;
  vector<ll>a;
  rep(i,0,n){
    ll x;
    cin>>x;
    a.pb(x);
  }
  string s;
  cin>>s;
  ll sum=0;
  char ch=s[0];
  vector<ll>val;
  rep(i,0,n){
    debug(val);
    if(ch==s[i]){
       val.pb(a[i]);
    }
    else{
        sort(all(val),greater<ll>());
        for(int j=0;j<val.size()&&j<k;j++){
          sum+=val[j];
        }  
       ch=s[i];
       val.clear();
       val.pb(a[i]);
    }
  }
  if(sz(val)!=0){
     sort(all(val),greater<ll>());
      for(int j=0;j<val.size()&&j<k;j++){
          sum+=val[j];
        }  
  }
  cout<<sum;
  
}
/*----------------------------------------------------------------------------------------------------------------------------*/
int main()
{
#ifndef ONLINE_JUDGE
  freopen("Error.txt", "w", stderr);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
#ifndef ONLINE_JUDGE
  freopen("Input.txt", "r", stdin);
  freopen("Output.txt", "w", stdout);
#endif
  ll testcases=1;
 // cin>>testcases;
  while(testcases--){
    solve();
  }
  return 0;
}


Comments

Submit
0 Comments
More Questions

1302. Deepest Leaves Sum
1209. Remove All Adjacent Duplicates in String II
994. Rotting Oranges
983. Minimum Cost For Tickets
973. K Closest Points to Origin
969. Pancake Sorting
967. Numbers With Same Consecutive Differences
957. Prison Cells After N Days
946. Validate Stack Sequences
921. Minimum Add to Make Parentheses Valid
881. Boats to Save People
497. Random Point in Non-overlapping Rectangles
528. Random Pick with Weight
470. Implement Rand10() Using Rand7()
866. Prime Palindrome
1516A - Tit for Tat
622. Design Circular Queue
814. Binary Tree Pruning
791. Custom Sort String
787. Cheapest Flights Within K Stops
779. K-th Symbol in Grammar
701. Insert into a Binary Search Tree
429. N-ary Tree Level Order Traversal
739. Daily Temperatures
647. Palindromic Substrings
583. Delete Operation for Two Strings
518. Coin Change 2
516. Longest Palindromic Subsequence
468. Validate IP Address
450. Delete Node in a BST